﻿using System.Windows.Forms;
using Reg = Microsoft.Win32.Registry;

namespace PowerManager.Settings
{
    public class RegistryHelper : IRegistryHelper
    {
        public void AddToAutoRun(string appName)
        {
            // The path to the key where Windows looks for startup applications
            var rkApp = Reg.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

            if (!IsStartupItem(appName))
                // Add the value in the registry so that the application runs at startup
                rkApp.SetValue(appName, Application.ExecutablePath);
        }

        public void DelFromAutoRun(string appName)
        {
            // The path to the key where Windows looks for startup applications
            var rkApp = Reg.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

            if (IsStartupItem(appName))
                // Remove the value from the registry so that the application doesn't start
                rkApp.DeleteValue(appName, false);
        }

        public bool IsStartupItem(string appName)
        {
            // The path to the key where Windows looks for startup applications
            var rkApp = Reg.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

            return rkApp != null && rkApp.GetValue(appName) != null;
        }
    }
}
